import {createRouter, createWebHistory} from "vue-router";
import store from '@/store'
import Home from "../views/Home.vue";

const routes = [
    {
        path: '/',
        redirect: '/dashboard'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: '/403',
                meta: {
                    title: '没有权限'
                },
                component: () => import('../views/error/403.vue')
            }, {
                path: '/:pathMatch(.*)*',
                meta: {
                    title: '找不到页面'
                },
                component: () => import('../views/error/404.vue')
            },
            {
                path: "/dashboard",
                meta: {
                    title: '工作台'
                },
                component: () => import("../views/Dashboard.vue")
            }, {
                path: "/system/page",
                meta: {
                    title: '系统管理'
                },
                component: () => import("../views/mgt/power/SystemList.vue")
            }, {
                path: "/system/operate",
                meta: {
                    title: '系统操作'
                },
                component: () => import("../views/mgt/power/SystemOperate.vue")
            }, {
                path: "/client/page",
                meta: {
                    title: '客户端管理'
                },
                component: () => import("../views/mgt/system/ClientList.vue")
            }, {
                path: "/client/operate",
                meta: {
                    title: '客户端操作'
                },
                component: () => import("../views/mgt/system/ClientOperate.vue")
            }, {
                path: "/server/page",
                meta: {
                    title: '服务管理'
                },
                component: () => import("../views/mgt/system/ServerList.vue")
            }, {
                path: "/server/operate",
                meta: {
                    title: '服务操作'
                },
                component: () => import("../views/mgt/system/ServerOperate.vue")
            }, {
                path: "/serverApi/page",
                meta: {
                    title: '服务API管理'
                },
                component: () => import("../views/mgt/system/ServerApiList.vue")
            }, {
                path: "/serverApi/operate",
                meta: {
                    title: '服务API操作'
                },
                component: () => import("../views/mgt/system/ServerApiOperate.vue")
            }, {
                path: "/i18nMsg/page",
                meta: {
                    title: '国际化消息'
                },
                component: () => import("../views/mgt/system/I18nMsgList.vue")
            }, {
                path: "/i18nMsg/operate",
                meta: {
                    title: '国际化消息操作'
                },
                component: () => import("../views/mgt/system/I18nMsgOperate.vue")
            }, {
                path: "/apiWhiteList/page",
                meta: {
                    title: 'API白名单'
                },
                component: () => import("../views/mgt/system/ApiWhiteListList.vue")
            }, {
                path: "/apiWhiteList/operate",
                meta: {
                    title: 'API白名单操作'
                },
                component: () => import("../views/mgt/system/ApiWhiteListOperate.vue")
            }, {
                path: "/ipBlackList/page",
                meta: {
                    title: 'IP黑名单'
                },
                component: () => import("../views/mgt/system/IpBlackListList.vue")
            }, {
                path: "/ipBlackList/operate",
                meta: {
                    title: 'IP黑名单操作'
                },
                component: () => import("../views/mgt/system/IpBlackListOperate.vue")
            }, {
                path: "/appCert/page",
                meta: {
                    title: '应用凭证'
                },
                component: () => import("../views/mgt/system/AppCertList.vue")
            }, {
                path: "/appCert/operate",
                meta: {
                    title: '应用凭证操作'
                },
                component: () => import("../views/mgt/system/AppCertOperate.vue")
            }, {
                path: "/thirdBindGrant/page",
                meta: {
                    title: '第三方授权管理'
                },
                component: () => import("../views/mgt/system/ThirdBindGrantList.vue")
            }, {
                path: "/thirdBindGrant/detail",
                meta: {
                    title: '第三方授权管理详情'
                },
                component: () => import("../views/mgt/system/ThirdBindGrantDetail.vue")
            }, {
                path: "/user/page",
                meta: {
                    title: '用户管理'
                },
                component: () => import("../views/mgt/power/UserList.vue")
            }, {
                path: "/user/operate",
                meta: {
                    title: '用户操作'
                },
                component: () => import("../views/mgt/power/UserOperate.vue")
            }, {
                path: "/role/page",
                meta: {
                    title: '角色管理'
                },
                component: () => import("../views/mgt/power/RoleList.vue")
            }, {
                path: "/role/operate",
                meta: {
                    title: '角色操作'
                },
                component: () => import("../views/mgt/power/RoleOperate.vue")
            }, {
                path: "/menu/list",
                meta: {
                    title: '菜单管理'
                },
                component: () => import("../views/mgt/power/MenuList.vue")
            }, {
                path: "/resource/page",
                meta: {
                    title: '资源管理'
                },
                component: () => import("../views/mgt/power/ResourceList.vue")
            }, {
                path: "/department/list",
                meta: {
                    title: '部门管理'
                },
                component: () => import("../views/mgt/power/DepartmentList.vue")
            }, {
                path: "/position/page",
                meta: {
                    title: '职位管理'
                },
                component: () => import("../views/mgt/power/PositionList.vue")
            }, {
                path: "/userGroup/page",
                meta: {
                    title: '用户组'
                },
                component: () => import("../views/mgt/power/UserGroupList.vue")
            }, {
                path: "/userGroup/operate",
                meta: {
                    title: '用户组操作'
                },
                component: () => import("../views/mgt/power/UserGroupOperate.vue")
            }, {
                path: "/org/page",
                meta: {
                    title: '组织管理'
                },
                component: () => import("../views/mgt/power/OrgList.vue")
            }, {
                path: "/org/operate",
                meta: {
                    title: '组织操作'
                },
                component: () => import("../views/mgt/power/OrgOperate.vue")
            }, {
                path: "/org/department",
                meta: {
                    title: '组织部门'
                },
                component: () => import("../views/mgt/power/OrgDepartment.vue")
            }, {
                path: "/region/list",
                meta: {
                    title: '省市区管理'
                },
                component: () => import("../views/setup/RegionList.vue")
            }, {
                path: "/oss/config/page",
                meta: {
                    title: '对象存储配置'
                },
                component: () => import("../views/setup/OssConfigList.vue")
            }, {
                path: "/oss/config/operate",
                meta: {
                    title: '对象存储操作'
                },
                component: () => import("../views/setup/OssConfigOperate.vue")
            }, {
                path: "/oss/tpl/page",
                meta: {
                    title: '对象存储模板配置'
                },
                component: () => import("../views/setup/OssTplList.vue")
            }, {
                path: "/oss/tpl/operate",
                meta: {
                    title: '对象存储模板配置操作'
                },
                component: () => import("../views/setup/OssTplOperate.vue")
            }, {
                path: "/sms/config/page",
                meta: {
                    title: '短信配置'
                },
                component: () => import("../views/setup/SmsConfigList.vue")
            }, {
                path: "/sms/config/operate",
                meta: {
                    title: '短信配置操作'
                },
                component: () => import("../views/setup/SmsConfigOperate.vue")
            }, {
                path: "/sms/tpl/page",
                meta: {
                    title: '短信模板配置'
                },
                component: () => import("../views/setup/SmsTplList.vue")
            }, {
                path: "/sms/tpl/operate",
                meta: {
                    title: '短信模板配置操作'
                },
                component: () => import("../views/setup/SmsTplOperate.vue")
            }, {
                path: "/vod/config/page",
                meta: {
                    title: '视频点播配置'
                },
                component: () => import("../views/setup/VodConfigList.vue")
            }, {
                path: "/vod/config/operate",
                meta: {
                    title: '视频点播配置操作'
                },
                component: () => import("../views/setup/VodConfigOperate.vue")
            }, {
                path: "/wechat/config/ma/page",
                meta: {
                    title: '微信小程序配置'
                },
                component: () => import("../views/setup/WechatConfigMaList.vue")
            }, {
                path: "/wechat/config/ma/operate",
                meta: {
                    title: '微信小程序配置操作'
                },
                component: () => import("../views/setup/WechatConfigMaOperate.vue")
            }, {
                path: "/wechat/config/mp/page",
                meta: {
                    title: '微信公众号配置'
                },
                component: () => import("../views/setup/WechatConfigMpList.vue")
            }, {
                path: "/wechat/config/mp/operate",
                meta: {
                    title: '微信公众号配置操作'
                },
                component: () => import("../views/setup/WechatConfigMpOperate.vue")
            }, {
                path: "/mail/config/page",
                meta: {
                    title: '邮箱配置'
                },
                component: () => import("../views/setup/MailConfigList.vue")
            }, {
                path: "/mail/config/operate",
                meta: {
                    title: '邮箱配置操作'
                },
                component: () => import("../views/setup/MailConfigOperate.vue")
            }, {
                path: "/flow/category/page",
                meta: {
                    title: '流程分类'
                },
                component: () => import("../views/flow/FlowCategoryList.vue")
            }, {
                path: "/flow/category/operate",
                meta: {
                    title: '流程分类操作'
                },
                component: () => import("../views/flow/FlowCategoryOperate.vue")
            }, {
                path: "/flow/form/page",
                meta: {
                    title: '流程表单'
                },
                component: () => import("../views/flow/FlowFormList.vue")
            }, {
                path: "/flow/form/operate",
                meta: {
                    title: '流程表单操作'
                },
                component: () => import("../views/flow/FlowFormOperate.vue")
            }, {
                path: "/flow/model/page",
                meta: {
                    title: '流程模型'
                },
                component: () => import("../views/flow/FlowModelList.vue")
            }, {
                path: "/flow/model/operate",
                meta: {
                    title: '流程模型操作'
                },
                component: () => import("../views/flow/FlowModelOperate.vue")
            }, {
                path: "/mail/tpl/page",
                meta: {
                    title: '邮箱模板配置'
                },
                component: () => import("../views/setup/MailTplList.vue")
            }, {
                path: "/mail/tpl/operate",
                meta: {
                    title: '邮箱模板配置操作'
                },
                component: () => import("../views/setup/MailTplOperate.vue")
            }, {
                path: "/gateway/log/page",
                meta: {
                    title: '网关日志'
                },
                component: () => import("../views/log/GatewayLogList.vue")
            }, {
                path: "/gateway/log/detail",
                meta: {
                    title: '网关日志详情'
                },
                component: () => import("../views/log/GatewayLogDetail.vue")
            }, {
                path: "/login/log/page",
                meta: {
                    title: '登录日志'
                },
                component: () => import("../views/log/LoginLogList.vue")
            }, {
                path: "/login/log/detail",
                meta: {
                    title: '登录日志详情'
                },
                component: () => import("../views/log/LoginLogDetail.vue")
            }, {
                path: "/operation/log/page",
                meta: {
                    title: '操作日志'
                },
                component: () => import("../views/log/OperationLogList.vue")
            }, {
                path: "/operation/log/detail",
                meta: {
                    title: '操作日志详情'
                },
                component: () => import("../views/log/OperationLogDetail.vue")
            }, {
                path: "/mail/log/page",
                meta: {
                    title: '邮箱日志'
                },
                component: () => import("../views/log/MailLogList.vue")
            }, {
                path: "/mail/log/detail",
                meta: {
                    title: '邮箱日志详情'
                },
                component: () => import("../views/log/MailLogDetail.vue")
            }, {
                path: "/mail/captcha/log/page",
                meta: {
                    title: '邮箱验证码日志'
                },
                component: () => import("../views/log/MailCaptchaLogList.vue")
            }, {
                path: "/sms/captcha/log/page",
                meta: {
                    title: '短信验证码日志'
                },
                component: () => import("../views/log/SmsCaptchaLogList.vue")
            }, {
                path: "/message",
                meta: {
                    title: '消息'
                },
                component: () => import("../views/Message.vue")
            }, {
                path: "/mail/send",
                meta: {
                    title: '邮件发送'
                },
                component: () => import("../views/util/MailSend.vue")
            }, {
                path: "/ws/send",
                meta: {
                    title: '消息推送'
                },
                component: () => import("../views/util/WsSend.vue")
            }
        ]
    }, {
        path: "/login",
        meta: {
            title: '登录'
        },
        component: () => import("../views/Login.vue")
    }, {
        path: "/bindUser",
        meta: {
            title: '绑定用户'
        },
        component: () => import("../views/BindUser.vue")
    }, {
        path: "/joinChatGroup",
        meta: {
            title: '扫码加群'
        },
        component: () => import("../views/im/JoinChatGroup.vue")
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title}`;
    const userToken = localStorage.getItem('userToken');
    if (to.path === '/bindUser') {
        next();
    } else if (!userToken && to.path !== '/login') { // 为登陆拦截，当本地存储没有数据当时候 跳转到登陆
        next('/login');
    } else {
        next();
    }
});

// 进入某一页 并关闭当前tag
router.goPage = function () {
    router.push(...arguments)
    closeTag();
}

// 返回上一页 并关闭当前标签页
router.goBack = function () {
    router.back()
    closeTag();
}

function closeTag() {
    store.commit("closeCurrentTag", {
        $router: router.getRoutes(),
        $route: router.currentRoute.value
    });
}

export default router;
